<template>
	<view class="box">

		<swiper class="swiper">

			<swiper-item class="sitem" v-for="q in quizzes">
				<!-- 测试进度条 -->
				<view class="progress">
					<view class="progress-box">
						<cwg-progress class="progress" :progress="q.qnum * 100 / count" :progressHeight="12"
							:border-radius="30" backgroundColor="#fafafa" bottomBackground="#fafafa" firstBackground ="#3f85ff" />

					</view>
				</view>
				
				<view class="question">
					<uni-card class="card" :is-shadow= "true" :title="`第` + q.qnum + `题`">
					<text class="questionText">{{q.question}}</text>
					</uni-card>
				</view>
				
				<view class="select">
					<uni-section title="选项" type="line" titleFontSize="28px"></uni-section>
					
					<view class="allOption">
							<radio-group @change="radioChange($event,q.qnum)">
						<view class="option" v-for="(o, index) in q.options">
						
								<!-- 弹性布局 -->
								<label class="uni-list-cell uni-list-cell-pd"  :key="o.miaoshu">
									<view class="optionBox">
										<radio   class="radio" :value="''+o.score"  :checked="index === current[q.qnum-1]"/>
										<view class="select">{{o.miaoshu}}</view>
									</view>
								</label>
							<view class="kongge"></view>
						</view>
						
						</radio-group>
						
					</view>
				</view>
			
			
			</swiper-item>
			
	<swiper-item>
		<view v-if="flag!=0">
<!-- 		<text>
			您还有第 {{flag}} 题没写
		</text> -->
	    </view>
		
		<!-- <text v-if="fenshu>=test.tZhuyi&&fenshu<=test.tZixun">您有抑郁倾向请多注意</text> -->
		
		<view v-if="fenshu>=test.tZixun ">
		<text >测试结果: {{content}}</text></br>
		<text >测试分数: {{totalScore}}</text>
		</view>
		
		<view v-if="fenshu==0"> 
		<button @click.prevent="getTotal">提交</button>
		<button @click="goZixun()"> 寻找咨询</button>
	</view>
</swiper-item>

		</swiper>
		

	</view>
</template>

<script>
	export default {
		data() {
			return {
				id:'1',
				quizzes: [],
				score: [],
				count: 0, //总题数
				total: 0 ,//计算每题得分之和再乘以系数
				current: [],
				test:{},
				fenshu:0,
				flag:0,
				totalScore : 0,
				content : '请先提交'
			}
		},
		onLoad(option) {
			this.id=option.tid;
			console.log(this.id)
		},
		created() {
			this.getTests();
			this.initScore();
		},
		methods: {
			/* 数据库还要加的字段 题目总数加到test 题目的 */
			//请求获取试卷 method默认为get
			getTests() {
				var a =this;
				uni.request({
					url: 'http://127.0.0.1:800/mhs/test/getTest/'+ a.id,

					success: (res) => {
						this.quizzes = res.data.data.quizzes;
						this.count = res.data.data.ttotal;
						this.test=res.data.data;
						//score[0]=0;
						console.log(this.quizzes);
						console.log(res.data.data);
					}
				})
			},
			//选项的改变事件 记录每题的得分
			radioChange(e, qnum) {
				console.log("触发改变事件" + e.detail.value);
				console.log(e);
				this.score[qnum - 1] = e.detail.value;
				//console.log(this.score[qnum - 1]);
				//console.log( this.quizzes[qnum-1].options.length )

				for (let i = 0; i < this.quizzes[qnum-1].options.length; i++) {
					//console.log(i);
					if (this.quizzes[qnum-1].options[i].score === e.detail.value) {
						this.current[qnum -1] = i;
						console.info(this.current);
						break;
						
					}
				}
			},
			//计算这个测试的总得分
			getTotal() {
				this.total = 0;
				
					
					console.info(this.score) 
					console.info(this) 
					for (var i in this.score) {
						if (this.score[i] == -1) {
							 this.flag = parseInt(i)+ 1;
						//	alert("请完成所有题目后提交/r/n" + "还有第" + a + "题没写");
							break;
						}
					this.total += parseInt(this.score[i]);
					//console.log(this.total)
					
				this.fenshu = this.total*this.test.tXishu;
					
				}
				let testSubmitVO = {
				  id: this.id,
				  chooses: this.score
				}
				uni.request({
				  url: 'http://127.0.0.1:800/mhs/test/submitTest', //仅为示例，并非真实的资源
				  method: 'POST',
				  header: {
				    // 'token': uni.getStorageSync('token')
				  },
				  data: testSubmitVO,
				  success: (res) => {
				    if (res.data.code == 1) {
				      uni.showToast({
				        title: '提交成功!' + res.data.data,
				        duration: 2000,
				        mask: true //是否有透明蒙层，默认为false 
				      })
					  this.totalScore = res.data.data;
					  if(this.totalScore >= 60){
						  this.content = '您当前心理健康状态良好，请继续保持'
					  } else{
						  this.content = '您抑郁倾向严重可以寻找咨询师咨询'
					  }
				    }else{
						uni.showModal({
							title: '提交失败',
							content: res.data.msg,
							success: function (res) {
								if (res.confirm) {
									// console.log('用户点击确定');
								} else if (res.cancel) {
									// console.log('用户点击取消');
								}
							}
						});
					}
				  }
				});
				// console.log(this.fenshu)
			},
			initScore() {
				console.log("初始化")
				for (var i = 0; i < 20; i++) {
					this.score[i] = -1;

				}

			},
			goZixun(){
				uni.navigateTo({
					url:"/pages/consultation/consultation"
				})
			}
		},
	}
</script>

<style>
	.kongge{
		height: 5vh;
	}
	.questionText{
		
		font-weight:800;
	}
	.swiper {
		width: 750rpx;
		height: 1200rpx;	
	}
	.select {
			display: inline-block;
		}

	.sitem {
		display: flex;
		flex-direction: column;
		justify-content: space-between;


	}

	.radio {

		display: inline-block;
	}

	.allOption {
		height: 600rpx;
		display: flex;
		flex-direction: column;
		justify-content: space-around;

	}
	.progress{
		width: 500rpx;
		margin-left: 70rpx;
		margin-top: 20rpx;
	}

	.optionBox {
		display: flex;
		
		
		
	}
</style>